package cn.com.scan.activity;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.text.method.NumberKeyListener;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.Calendar;
import java.util.HashMap;
import java.util.List;

import cn.com.scan.R;
import cn.com.scan.entity.Check;
import cn.com.scan.jsonparser.ParseJson;
import cn.com.scan.netTools.RequestWebServive;
import cn.com.scan.netTools.ResultCode;
import cn.com.scan.tools.DialogUtils;

/**
 * Created by My on 2016/1/6.
 */
public class CheckActivity extends  BaseActivity{
    private String ReturnCheckExp="ReturnCheckExp";
    private String SubmitCheck="SubmitCheck";
    private int ReturnCheckExpTAG=100;
    private int SubmitCheckTAG=101;
    EditText editText1,editText2,editText3,editText4,editText5,editText6,editText7,
            editText8,editText9,editText10;
    Check check;
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            DialogUtils.closeProgressDialog();
            if (msg.what == ResultCode.SUCCESS) {
                handlerDisPose(msg);
            } else if (msg.what == ResultCode.IOFAILCODE) {
                Toast.makeText(CheckActivity.this, "服务器连接失败！", Toast.LENGTH_SHORT).show();
            } else if (msg.what == ResultCode.PARSERFAILCODE) {
                Toast.makeText(CheckActivity.this, "数据解析出错！", Toast.LENGTH_SHORT).show();
            } else {
                Toast.makeText(CheckActivity.this, "未知错误！", Toast.LENGTH_SHORT).show();
            }
        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.check_activity);
        editText1 =(EditText)findViewById(R.id.editText1);
        editText2 =(EditText)findViewById(R.id.editText2);
        editText3 =(EditText)findViewById(R.id.editText3);
        editText4 =(EditText)findViewById(R.id.editText4);
        editText5 =(EditText)findViewById(R.id.editText5);
        editText6 =(EditText)findViewById(R.id.editText6);
        editText7 =(EditText)findViewById(R.id.editText7);
        editText8 =(EditText)findViewById(R.id.editText8);
        editText9 =(EditText)findViewById(R.id.editText9);
        editText10 =(EditText)findViewById(R.id.editText10);
        findViewById(R.id.commitButton).setOnClickListener(onclick);
        findViewById(R.id.cancleButton).setOnClickListener(onclick);
        editText5.setKeyListener(watcher);
        TextView user_name =(TextView)findViewById(R.id.user_name);
        user_name.setText(user.getNAME());
//        HashMap<String, Object> map=new   HashMap<String, Object>();
//        map.put("barcode","YWT012345567890123456781,01,1502005222,30001,33-44-55-66-71,2016/12/31,2015/01/01,2015/02/01,FH2016010300001");
//        requestNet(ReturnCheckExp, map, ReturnCheckExpTAG);

    }

    public void requestNet(String method, HashMap<String, Object> map, int TAG) {
        DialogUtils.showProgressDialog(CheckActivity.this);
        RequestWebServive requestWebServive = new RequestWebServive(method, handler, map, TAG);
        requestWebServive.start();
    }
    public void handlerDisPose(Message msg) {
        String str = (String) msg.obj;
        if (!TextUtils.isEmpty(str)) {
            try {
                JSONObject Json = new JSONObject(str);
                int Code = Json.getInt("CODE");
                if (Code == 0) {
                    String RESULT = Json.getString("RESULT");
                    if (msg.arg1 == ReturnCheckExpTAG) {
                        List<Check> checks= ParseJson.parseCheck(RESULT);
                        check=checks.get(0);
                        editText1.setText(check.getEXP_CODE());
                        editText2.setText(check.getEXP_NAME());
                        editText3.setText(check.getEXP_SPEC());
                        editText4.setText(check.getUNIT_NAME());
                        editText5.setText(check.getCHECK_QUANTITY());
                        editText6.setText(check.getFIRM_NAME());
                        editText7.setText(check.getCHECK_MAN());
                        editText10.setText(check.getQUANTITY());
                        if(check.getDATA_FLAG().equals("0") ){
                            editText8.setText("源数据");
                        }else{
                            editText8.setText("新数据");
                        }
                        if(check.getUPDATE_FLAG().equals("0")){
                            editText9.setText("未盘点");
                        }else{
                            editText9.setText("已盘点");
                        }
                    } else if (msg.arg1 == SubmitCheckTAG){
                        Toast.makeText(CheckActivity.this, "修改成功！", Toast.LENGTH_SHORT).show();
                        editText1.setText("-------------");
                        editText2.setText("-------------");
                        editText3.setText("-------------");
                        editText4.setText("-------------");
                        editText5.setText("");
                        editText6.setText("-------------");
                        editText7.setText("-------------");
                        editText8.setText("-------------");
                        editText9.setText("-------------");
                        editText10.setText("-------------");

                    }
                } else {
                    Toast.makeText(CheckActivity.this, Json.getString("MSG"), Toast.LENGTH_SHORT).show();
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }
    private View.OnClickListener onclick = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.commitButton:
                    showDialog();
                break;
                case R.id.cancleButton:
                    CheckActivity.this.finish();
                break;

            }

        }
    };
    private void  showDialog(){
        AlertDialog.Builder alertBuilder = new AlertDialog.Builder(CheckActivity.this);
        alertBuilder.setTitle("提示");
        alertBuilder.setMessage("确定要修改盘点的数据？");
        alertBuilder.setPositiveButton("确定",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface arg0, int arg1) {
                        arg0.dismiss();
                        HashMap<String, Object> map = new HashMap<String, Object>();
                        if(check==null){
                            Toast.makeText(CheckActivity.this,"数据不存在",Toast.LENGTH_SHORT).show();
                            return;
                        }
                        map.put("barcode", check.getBARCODE());
                        map.put("userName", user.getUSER_NAME());
                        map.put("quantity", editText5.getText().toString());
                        requestNet(SubmitCheck, map, SubmitCheckTAG);
                    }
                });
        alertBuilder.setNegativeButton("取消",
                new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface arg0, int arg1) {
                        arg0.dismiss();
                    }
                });
        alertBuilder.show();
    }
    @Override
    protected void broadcastExit() {
        this.finish();
    }

    @Override
    protected void capturePic(String codeStr) {
        HashMap<String, Object> map=new   HashMap<String, Object>();
        map.put("barcode",codeStr);
        requestNet(ReturnCheckExp, map,ReturnCheckExpTAG);
    }
    private NumberKeyListener watcher = new NumberKeyListener() {
        @Override
        public int getInputType() {
            return android.text.InputType.TYPE_CLASS_NUMBER;
        }

        @Override
        protected char[] getAcceptedChars() {
            return new char[]{'1', '2', '3', '4', '5', '6', '7', '8',
                    '9', '0', '.'};
        }
    };
}
